﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ADA.Entity;
using ADA.Data;
using System.Data.SqlClient;
using System.IO;

namespace ADA.Business
{
    public class BOLNews
    {
        public bool viewAllRice(out DataTable tblRice, out string message)
        {
            tblRice = new DataTable();
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                tblRice = dalRice.getAllRice();
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool viewAllRicePrice(out DataTable tblRicePrice, out string message)
        {
            tblRicePrice = new DataTable();
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                tblRicePrice = dalRice.getAllRicePrice();
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool viewAllNews(out DataTable tblNews, out string message)
        {
            tblNews = new DataTable();
            message = "";
            DALNews dalNews = new DALNews();
            try
            {
                tblNews = dalNews.getAllNews();
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool updatePrice(int riceID, int newPrice, out string message)
        {
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                dalRice.updatePrice(riceID, newPrice);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool updatePercent(int riceID, int newPercent, out string message)
        {
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                dalRice.updatePercent(riceID, newPercent);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool getRiceInfo(int riceID, out Rice rice, out string message)
        {
            rice = new Rice();
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                rice = dalRice.getRiceInfo(riceID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool addRice(Rice rice, out string message)
        {
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                dalRice.insertRice(rice);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool editRice(Rice rice, out string message)
        {
            message = "";
            DALRice dalRice = new DALRice();
            try
            {
                dalRice.updateRice(rice);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool deleteRice(int riceID, out string message)
        {
            message = "";
            DALWarehouse dalWh = new DALWarehouse();
            DALOrder dalOrder = new DALOrder();
            DALPost dalPost = new DALPost();
            DALRice dalRice = new DALRice();
            try
            {
                dalWh.deleteWarehouseByRiceID(riceID);
                dalOrder.deleteOrderToUserPost(riceID);
                dalPost.deletePostByRiceID(riceID);
                dalRice.deleteRice(riceID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool getNewsInfo(int newsID, out News news, out string message)
        {
            news = new News();
            message = "";
            DALNews dalNews = new DALNews();
            try
            {
                news = dalNews.getNewsInfo(newsID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool addNews(News news, out string message)
        {
            message = "";
            DALNews dalNews = new DALNews();
            try
            {
                dalNews.insertNews(news);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool editNews(News news, out string message)
        {
            message = "";
            DALNews dalNews = new DALNews();
            try
            {
                dalNews.updateNews(news);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool deleteNews(int newsID, out string message)
        {
            message = "";
            DALNews dalNews = new DALNews();
            try
            {
                dalNews.deleteNews(newsID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    }
}
